<?php

namespace App\Http\Requests;

use App\Util\Result;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use App\Enum\ShopType;

class FinanceRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'shop_id' => 'bail|required|min:1|integer',
            'house_id' => 'bail|required_if:shop_type,'.ShopType::OFFICE_BUILDING.'|integer',
            'rent_starttime' => 'bail|required|date:Y-m-d',
            'rent_endtime' => 'bail|required|date:Y-m-d',
            'rent_money' => 'bail|required|numeric',
            'name' => 'required',
            'tel' => ['bail','required','regex:/^1[3|4|5|7|8][0-9]{9}$/'],
            'deal_time' => 'bail|required|date:Y-m-d'
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'shop_id.required' => '请选择商铺名称',
            'shop_id.min' => '请选择商铺名称',
            'shop_id.integer'  => '商铺id有误',
            'house_id.required_if' => '请选择户型名称',
            'house_id.min' => '请选择户型名称',
            'house_id.integer' => '户型id有误',
            'rent_starttime.required' => '请填写租房起始时间',
            'rent_starttime.date' => '请填写正确的租房起始时间',
            'rent_endtime.required' => '请填写租房到期时间',
            'rent_endtime.date' => '请填写正确的租房到期时间',
            'rent_money.required' => '请填写租房金额',
            'rent_money.numeric' => '租房金额必须为数字',
            'name.required' => '请填写联系人姓名',
            'tel.required' => '请填写联系电话',
            'tel.regex' => '请填写正确的联系电话',
            'deal_time.required' => '请填写成交时间',
            'deal_time.date' => '请填写正确的成交时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return Result::failure( $validator->errors()->first() )->getResult();
    }
}
